define(function (require) {
    var ws;
    var $ = require("jquery");
    require("jquery.cookie");

    function load() {


        ws = new WebSocket(require("API").webSocketServer($.cookie("courseId")));
        ws.addEventListener("message", function (e) {
            console.log("receive   ", JSON.parse(e.data))
        })
        ws.addEventListener("error", function (e) {
            console.log("ws error   ", (e));
        })
        ws.addEventListener("close", function (e) {
            console.log("ws close   ", e);
            if ($.cookie('courseId') === undefined) {
                // alert('请选择课程')
                require('toastDom').showMsg("请选择课程",3000);
            } else {
                // alert("websocket失去连接  请刷新页面重连")
                require('toastDom').showMsg("websocket失去连接  请刷新页面重连",3000);
            }
        });
    }

    load();
    // 心跳包  防止websocket自动断开
    setInterval(function () {
        ws.send(JSON.stringify({type: 1}));
    }, 30000);


    return ws;
})